package com.xo.so.jsouplocal;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.xo.so.Variables;
import com.xo.so.model.KetQuaXoSoMienBac;
import com.xo.so.model.KetQuaXoSoMienNam;
import com.xo.so.model.KetQuaXoSoMienTrung;

public final class JsoupLocal {

	private static final String KQXSMB_URL = "http://xoso.wap.vn/ket-qua-xo-so-mien-bac-xstd.html";
	private static final String TITLE_SELECTOR = ".XS h2";
	private static final String ROW_1_SELECTOR = ".XS .XSMB_ngang";
	private static final String ROW_2_SELECTOR = ".XS .LOTO_ngang_1";
	private static final String CELL_1_SELECTOR = "td:not(.XSMB_1)";
	private static final String CELL_2_SELECTOR = "td:not(.LOTO_1)";

	private JsoupLocal() {

	}

	public static KetQuaXoSoMienBac parseKQXSMB() {
		KetQuaXoSoMienBac result = new KetQuaXoSoMienBac();

		try {
			Connection connect = Jsoup.connect(KQXSMB_URL)
					.timeout(Variables.OUT).ignoreHttpErrors(true);
			Variables.document = connect.get();

			String title = Variables.document.select(TITLE_SELECTOR).text();
			result.setTitle(title);

			Elements trTableThuNhat = Variables.document.select(ROW_1_SELECTOR);
			int giaiOBang1 = 0;
			int viTriOBang1 = 0;
			int n = 0;
			for (Element tr : trTableThuNhat) {
				Elements tdTableThuNhat = tr.select(CELL_1_SELECTOR);
				for (Element td : tdTableThuNhat) {
					String maSo = td.text();
					result.setMaSoTrungThuong(giaiOBang1, viTriOBang1, maSo);
					n++;
					viTriOBang1++;
				}
				if (n == result.SO_GIAI[giaiOBang1]) {
					n = 0;
					giaiOBang1++;
					viTriOBang1 = 0;
				}
			}
			Elements trTableThuHai = Variables.document.select(ROW_2_SELECTOR);

			int viTriOBang2 = 0;
			int m = 0;
			for (Element tr : trTableThuHai) {
				Elements tdTableThuHai = tr.select(CELL_2_SELECTOR);
				for (Element td : tdTableThuHai) {
					String duoi = td.text();

					result.setCacSoDaDuoiMaGiai(viTriOBang2, duoi);
					if (m % 2 == 0) {
						viTriOBang2 += 5;
					} else {
						viTriOBang2 -= 4;
					}
					m++;

				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			Variables.Error = true;

		}
		return result;

	}

	public static KetQuaXoSoMienTrung parseKQXSMT(String link) {
		KetQuaXoSoMienTrung result = new KetQuaXoSoMienTrung();

		try {
			Connection connect = Jsoup.connect(link).timeout(Variables.OUT)
					.ignoreHttpErrors(true);
			Variables.document = connect.get();

			String title = Variables.document.select(TITLE_SELECTOR).text();
			result.setTitle(title);

			Elements trTableThuNhat = Variables.document.select(ROW_1_SELECTOR);
			int giaiOBang1 = 0;
			int viTriOBang1 = 0;
			int n = 0;
			for (Element tr : trTableThuNhat) {
				Elements tdTableThuNhat = tr.select(CELL_1_SELECTOR);
				for (Element td : tdTableThuNhat) {
					String maSo = td.text();
					result.setMaSoTrungThuong(giaiOBang1, viTriOBang1, maSo);
					n++;
					viTriOBang1++;
				}
				if (n == result.SO_GIAI[giaiOBang1]) {
					n = 0;
					giaiOBang1++;
					viTriOBang1 = 0;
				}
			}

			Elements trTableThuHai = Variables.document.select(ROW_2_SELECTOR);

			int viTriOBang2 = 0;
			int m = 0;
			for (Element tr : trTableThuHai) {
				Elements tdTableThuHai = tr.select(CELL_2_SELECTOR);
				for (Element td : tdTableThuHai) {
					String duoi = td.text();

					result.setCacSoDaDuoiMaGiai(viTriOBang2, duoi);
					if (m % 2 == 0) {
						viTriOBang2 += 5;
					} else {
						viTriOBang2 -= 4;
					}
					m++;

				}

			}

		} catch (Exception e) {
			e.printStackTrace();

		}
		return result;
	}

	public static KetQuaXoSoMienNam parseKQXSMN(String link) {
		KetQuaXoSoMienNam result = new KetQuaXoSoMienNam();

		try {
			Connection connect = Jsoup.connect(link).timeout(Variables.OUT)
					.ignoreHttpErrors(true);
			Variables.document = connect.get();

			String title = Variables.document.select(TITLE_SELECTOR).text();
			result.setTitle(title);

			Elements trTableThuNhat = Variables.document.select(ROW_1_SELECTOR);
			int giaiOBang1 = 0;
			int viTriOBang1 = 0;
			int n = 0;
			for (Element tr : trTableThuNhat) {
				Elements tdTableThuNhat = tr.select(CELL_1_SELECTOR);
				for (Element td : tdTableThuNhat) {
					String maSo = td.text();
					result.setMaSoTrungThuong(giaiOBang1, viTriOBang1, maSo);
					n++;
					viTriOBang1++;
				}
				if (n == result.SO_GIAI[giaiOBang1]) {
					n = 0;
					giaiOBang1++;
					viTriOBang1 = 0;
				}
			}

			Elements trTableThuHai = Variables.document.select(ROW_2_SELECTOR);

			int viTriOBang2 = 0;
			int m = 0;
			for (Element tr : trTableThuHai) {
				Elements tdTableThuHai = tr.select(CELL_2_SELECTOR);
				for (Element td : tdTableThuHai) {
					String duoi = td.text();

					result.setCacSoDaDuoiMaGiai(viTriOBang2, duoi);
					if (m % 2 == 0) {
						viTriOBang2 += 5;
					} else {
						viTriOBang2 -= 4;
					}
					m++;

				}

			}

		} catch (Exception e) {
			e.printStackTrace();

		}
		return result;
	}
}
